/*
 * SRMI (an easy-to-use zero-code remoting library for Java)
 * Copyright (C) 2007  Mohammad Nabil Hussein
 * mohammad (dot) nabil (dot) h (at) gmail (dot) com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package SRMI.Tests;

import java.lang.instrument.ClassDefinition;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
import java.security.ProtectionDomain;
import java.util.HashMap;

public class Agent {

    private static Instrumentation instrumentation;
    public static HashMap<String, byte[]> cache;
    
    public static void premain(String options, Instrumentation theInstrumentation) {
        instrumentation = theInstrumentation;
        instrumentation.addTransformer(new ClassFileTransformer() {            
            public byte[] transform(
                    ClassLoader loader,
                    String className,
                    Class<?> classBeingRedefined,
                    ProtectionDomain protectionDomain,
                    byte[] classfileBuffer) throws IllegalClassFormatException 
            {
                try 
                {
                    if(cache == null) 
                        cache = new HashMap<String,byte[]>();                    
                    cache.put(className, classfileBuffer);
                    if (!className.startsWith("SRMI") 
                            || className.contains("DefaultCtorNeutralizer")
                            || className.contains("BasicInstrumenter")
                            || classBeingRedefined != null) 
                        // if the classBeingRedefined != null, it means it have been loaded before
                        // and hence have passed here before, and it already has the plugs we want
                        // no need to plug them again
                        // this method is only to one-time plug the classes
                        return classfileBuffer;                    
                    
                    if (Config.verbose) System.out.println("Preprocessing " + className);
                    return new DefaultCtorNeutralizer(classfileBuffer, className).prepare();
                }
                catch (Throwable ex) 
                {
                    ex.printStackTrace();
                    return classfileBuffer;                            
                }
            }
        }, true);
    }
    
    public static void redefineClasses(ClassDefinition... defs) throws ClassNotFoundException, UnmodifiableClassException
    {
        if(instrumentation == null)
                throw new UnsupportedOperationException("Java 5 was not started with preMain -javaagent");
        instrumentation.redefineClasses(defs);
    }
}
